"""主界面程序"""
from PyQt5 import uic
from PyQt5.QtWidgets import QApplication, QMessageBox, QWidget
from covid_inquire import get_time
from auto_report import auto_report
import config_io
import threading
from PyQt5.QtCore import pyqtSignal, pyqtSlot

UI_path = "ui.ui"

DEFAULT_CONFIG_PATH = "default.json"
CONFIG_PATH = "config.json"


class MainWindow(QWidget):

    def __init__(self):
        super(MainWindow, self).__init__()
        # 加载UI
        self.ui = uic.loadUi(UI_path)
        # 连接信号
        self.message_critical_signal.connect(self._message_box)
        # 读取参数
        config_io.read_config(self, CONFIG_PATH)
        # 连接按钮
        self.ui.covid_inquire.clicked.connect(self.__covid_inquire)
        self.ui.auto_report.clicked.connect(self.__auto_report)
        self.ui.set_default.clicked.connect(self.__set_default)

    message_critical_signal = pyqtSignal(str, str)

    @pyqtSlot(str, str)
    def _message_box(self, title, content):
        QMessageBox.critical(self.ui, title, content)

    def __covid_inquire(self):
        """查询48小时核酸检测结果，并填写至界面"""
        # 查询48小时核酸检测结果，查找失败会报错
        try:
            covid_test_time = get_time()
        except ValueError as e:
            covid_test_time = None
            QMessageBox.critical(self.ui, "Error", e.args[0])
        # 根据查找结果填写界面
        if covid_test_time is None:
            self.ui.covid_test.setCurrentIndex(0)
            self.ui.covid_result_index.setCurrentIndex(0)
        else:
            self.ui.covid_test.setCurrentIndex(1)
            self.ui.covid_result_index.setCurrentIndex(1)
            self.ui.covid_test_time.setText(covid_test_time)
            QMessageBox.information(self.ui, "Information", "查询完毕！")

    def __auto_report(self):
        """按照界面内容填写健康每日报"""
        # 检查参数
        if self.ui.code_color.currentIndex() == 0 or self.ui.code_color.currentIndex() == 1:
            QMessageBox.critical(self.ui, "Error", "请仔细检查一码通颜色！")
            return
        if self.ui.covid_result_index.currentIndex() == 2:
            QMessageBox.critical(self.ui, "Error", "请仔细检查核酸检测结果是否为阳性!")
            return

        def run():
            try:
                auto_report(self.ui.id.text(), self.ui.password.text(), self.ui.title.currentText(),
                            self.ui.temperature.value(),
                            self.ui.covid_test.currentIndex(), self.ui.covid_result_index.currentIndex(),
                            self.ui.covid_test_time.text(),
                            self.ui.isolation.currentIndex(), self.ui.code_color.currentIndex())
            except ValueError as e:
                self.message_critical_signal.emit("Error", e.args[0])

        threading.Thread(target=run).start()

    def __set_default(self):
        config_io.read_config(self, DEFAULT_CONFIG_PATH)

    def about_to_quit(self):
        config_io.write_config(self, CONFIG_PATH)


if __name__ == '__main__':
    app = QApplication([])
    main_window = MainWindow()
    app.aboutToQuit.connect(main_window.about_to_quit)
    main_window.ui.show()
    app.exec_()
